import {Component} from '@angular/core';
import {NavController} from 'ionic-angular';
import {TabsPage} from "../tabs/tabs";
import {PwdPage} from "../pwd/pwd";
import {services} from "../../providers/service";

import 'rxjs/add/operator/toPromise';
import 'rxjs/add/operator/timeout';
import {LoginService} from "./login.service";
//import {HomePage} from "../home/home";

@Component({
    selector: 'login',
    templateUrl: 'login.html',
    providers: [LoginService]
})
export class LoginPage {

    loginError = false;

    constructor(public navCtrl: NavController,
                public service: services,
                public loginService: LoginService,) {

    }

    userInfo: UserInfo = {
        UserName: '',
        PassWord: ''
    }


    findPWD() {
        this.navCtrl.setRoot(PwdPage);
    }

    login() {
        let param = {
            username: this.userInfo.UserName,
            password: this.userInfo.PassWord
        };
        this.loginService.login(param)
            .then(data => {
                console.log(data);
                if (data.resultStatus == 'success') {
                    this.navCtrl.setRoot(TabsPage);
                }
                else {
                    this.loginError = true;
                }

            }, err => {
                console.log('登陆错误', err);
            })
            .catch(fail => {
                console.log('登录异常', fail);
            });
    }

}

export class UserInfo {
    UserName: string;
    PassWord: string;
}
